package com.example.myapplication.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.example.myapplication.bean.UserModel;

import org.androidannotations.annotations.AfterInject;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EBean;
import org.androidannotations.annotations.RootContext;

import java.util.List;

@EBean
public class UserListAdapter extends BaseAdapter {

    List<UserModel> userModels;

    @Bean(ImMemoryUserFinder.class)
    UserFinder userFinder;

    @RootContext
    Context context;

    @AfterInject
    void initAdapter() {
        userModels = userFinder.findAll();
    }

    @Override
    public int getCount() {
        return userModels.size();
    }

    @Override
    public Object getItem(int position) {
        return userModels.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        UserItemView userItemView;
        if (convertView == null) {
            userItemView = UserItemView_.build(context);
        } else {
            userItemView = (UserItemView) convertView;
        }
        userItemView.bind(userModels.get(position));
        return userItemView;
    }
}
